import shutil
import UnityPy
from utils.创建文件夹 import createFolder
from utils.文件夹下的所有文件 import getFilesList


def extract_file(src: str, dst: str):
    env = UnityPy.load(src)
    for obj in env.objects:
        obj_name = str(obj.container).split("/")[-1].split(".")[0].upper()
        if obj.type.name == "TextAsset":
            data = obj.read()
            # 编辑资源
            with open(f"output/IDS压缩/{obj_name}.txt", "rb") as f:
                data.script = f.read()
            data.save()

    createFolder(dst)  # 没有文件夹就创建一个
    with open(f"{dst}/{src.split('/')[-1]}", "wb") as f:  # 其实就是拿到文件名 写入而已
        f.write(env.file.save())


def packup(src_dir: str):
    file_names = getFilesList(src_dir)
    for file_name in file_names:
        src = f"{src_dir}/{file_name}"
        dst = f"packup/{file_name[:2]}"
        extract_file(src, dst)

    # 制作还原补丁 还原的结果放在backup中
    for file_name in file_names:
        src = f"{src_dir}/{file_name}"
        createFolder(f"backup/{file_name[:2]}")
        shutil.copy(src, f"backup/{file_name[:2]}/{file_name}")

createFolder("packup")
createFolder("backup")
packup("data/IDS/日文")
packup("data/IDS/英文")
print("success")
